//
//  JPillnessDescriptionView.m
//  JPKY
//
//  Created by Vitas on 2016/11/15.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "JPillnessDescriptionView.h"
#import "JPEdgeInsetsLabel.h"

@interface JPillnessDescriptionView () <UITextViewDelegate>

/**
 *  疾病描述标签
 */
@property (nonatomic, strong) UILabel *descLabel;

/**
 *  添加描述标签
 */
@property (nonatomic, strong) UIButton *addDescButton;

/**
 *  底部黑线View
 */
@property (nonatomic, strong) UIView *lineView;

/**
 *  加号图片
 */
@property (nonatomic, strong) UIButton * addPictureView;

/**
 *  疾病文本输入框
 */
@property (nonatomic, strong) UITextView *descTextVeiw;

/**
 *  装载默认的描述
 */
@property (nonatomic, strong) UIView *containtView;

@property (nonatomic, strong) UILabel *stringLabel;

/**
 *  下一个Label的frame
 */
@property (nonatomic, assign) CGRect lastFrame;

/**
 *  判断是否第二行Label
 */
@property (nonatomic, assign) BOOL isSecondLabel;

/**
 *  高度约束
 */
@property (nonatomic, strong) MASConstraint *heightConstraint;

@end

@implementation JPillnessDescriptionView

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

/**
 *  初始化UI
 */
- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.descLabel];
    [self addSubview:self.addPictureView];
    [self addSubview:self.lineView];
    [self addSubview:self.addDescButton];
    [self addSubview:self.descTextVeiw];
    [self addSubview:self.containtView];
    [self.descTextVeiw addSubview:self.stringLabel];
    
    // 自动布局子控件
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(10);
        make.top.mas_equalTo(self.mas_top).offset(10);
        
    }];
    
    [self.addPictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-10);
        make.top.mas_equalTo(self.mas_top).offset(7);
    }];
    
    [self.addDescButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.addPictureView.mas_left).offset(-5);
        make.top.equalTo(self.descLabel.mas_top).offset(-8);
    }];
    
    [self.containtView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(20);
        make.right.mas_equalTo(self.mas_right).offset(-20);
        make.top.mas_equalTo(self.descLabel.mas_bottom).offset(10);
        self.heightConstraint = make.height.mas_equalTo(0);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(10);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(screenW, 1));
    }];
    
    [self.descTextVeiw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(20);
        make.right.mas_equalTo(self.mas_right).offset(-20);
        make.top.mas_equalTo(self.containtView.mas_bottom).offset(15);
        make.bottom.mas_equalTo(self.lineView.mas_top).offset(-10);
    }];
}

#pragma mark - Setter方法

- (void)setStringArray:(NSMutableArray *)stringArray {
    _stringArray = stringArray;
    
    self.isSecondLabel = NO;
    // 没有选中时
    if (_stringArray.count == 0) {
        [self.heightConstraint uninstall];
        [self.containtView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
    self.lastFrame = CGRectZero;
    [self.containtView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    [_stringArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        JPEdgeInsetsLabel *label = [[JPEdgeInsetsLabel alloc] init];
        __weak typeof(label) weakLabel = label;
        // 设置内边距
        label.edgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        label.font= [UIFont fontWithName:@"HelveticaNeue-Thin" size:20];
        [label setDeleteFromSuperView:^(NSString *text) {
            //判断属性是否添加还是删除
            if (weakLabel.isDeleted) {
                [self.stringArray removeObject:text];
            } else {
                [self.stringArray addObject:text];
            }
        }];
        
        label.textColor = globalColor;
        label.text = obj;
        
        [label sizeToFit];
        label.layer.borderColor = globalColor.CGColor;
        label.layer.borderWidth = 3;
        
        CGFloat Width = label.frame.size.width;
        CGFloat height = label.frame.size.height;
        
        CGFloat tempHeight = 0;
        
        // 第二行改变高度
        if (self.isSecondLabel) {
            tempHeight = height * 2 + 10;
        } else {
            tempHeight = height;
        }
        
        // 更改约束
        [self.heightConstraint uninstall];
        [self.containtView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(tempHeight);
        }];
        
        label.frame = CGRectMake(self.lastFrame.size.width+self.lastFrame.origin.x +20 , tempHeight - height, Width, height);
        
        [self.containtView addSubview:label];
        
        self.lastFrame = label.frame;
        
        if (self.lastFrame.origin.x + label.frame.size.width +20 > screenW) {
            label.frame = CGRectMake(0+20, tempHeight +10                                                                                                             , Width, height);
            self.lastFrame = label.frame;
            
            self.isSecondLabel = YES;
            
            // 更改约束
            [self.heightConstraint uninstall];
            [self.containtView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo((height+10) * 2);
            }];
        }
    }];
}

- (void)setDesText:(NSString *)desText {
    _desText = desText;
    self.descTextVeiw.text = desText;
    self.stringLabel.text = @"";
}

#pragma mark - 响应事件

/**
 *  点击添加病例描述标签
 */
- (void)didClickAddDescButton:(UIButton *)button {
    if (self.didClickButton) {
        self.didClickButton(self.stringArray);
    }
}

#pragma mark - 代理方法

- (void)textViewDidChange:(UITextView *)textView {
    self.stringLabel.text =  self.descTextVeiw.text.length == 0 ? @"请输入你的病例情况":@"";
    if (textView.text.length >0) {
        if (self.callBackTextString) {
            self.callBackTextString(textView.text);
        }
    }
    
    if (textView.text.length ==0) {
        textView.text = nil;
    }
}

#pragma mark - 懒加载子控件

/**
 *  描述label
 */
- (UILabel *)descLabel {
    if (!_descLabel) {
        _descLabel = [[UILabel alloc] init];
        _descLabel.text = @"疾病描述";
    }
    return _descLabel;
}

/**
 *  添加描述button
 */
- (UIButton *)addDescButton {
    if (!_addDescButton) {
        _addDescButton = [[UIButton alloc] init];
        _addDescButton.titleLabel.numberOfLines = 0;
        _addDescButton.titleLabel.textAlignment = NSTextAlignmentRight;
        [_addDescButton addTarget:self action:@selector(didClickAddDescButton:) forControlEvents:UIControlEventTouchUpInside];
        [_addDescButton setTitle:@"添加病症描述标签" forState:UIControlStateNormal];
        [_addDescButton setTitleColor:globalColor forState:UIControlStateNormal];
    }
    return _addDescButton;
}

/**
 *  加号图片按钮
 */
- (UIButton *)addPictureView {
    if (!_addPictureView) {
        _addPictureView = [[UIButton alloc] init];
        [_addPictureView setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
        [_addPictureView addTarget:self action:@selector(didClickAddDescButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addPictureView;
}

/**
 *  底部分割线
 */
- (UIView *)lineView {
    
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _lineView;
}

/**
 *  输入框
 */
- (UITextView *)descTextVeiw {
    if (!_descTextVeiw) {
        _descTextVeiw = [[UITextView alloc] init];
        _descTextVeiw.delegate = self;
        _descTextVeiw.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    
    return _descTextVeiw;
}

/**
 *  装载标签栏
 */
- (UIView *)containtView {
    if (!_containtView) {
        _containtView = [[UIView alloc] init];
    }
    
    return _containtView;
}

/**
 *  占位Label
 */
- (UILabel *)stringLabel {
    if(!_stringLabel) {
        _stringLabel = [[UILabel alloc] init];
        _stringLabel.frame = CGRectMake(5, 5, 200, 20);
        _stringLabel.text = @"请输入你的病例情况";
        _stringLabel.font = [UIFont systemFontOfSize:15];
        _stringLabel.textColor = [UIColor darkGrayColor];
        [_stringLabel sizeToFit];
    }
    return _stringLabel;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.descTextVeiw resignFirstResponder];
}

@end
